<?php

namespace common\models;

use common\helpers\commonApi;
use common\helpers\TransformApi;
use common\helpers\ValidateApi;
use Yii;
use yii\db\Exception;

/**
 * This is the model class for table "{{%dis}}".
 *
 * @property int $id
 * @property int|null $userid 用户id
 * @property int|null $campus_id 校区id
 * @property string|null $name 姓名
 * @property string|null $phone 手机号
 * @property int|null $status 状态
 * @property int|null $review_status 审核状态
 * @property string|null $review_time 审核时间
 * @property string|null $review_remarks 审核备注
 * @property string|null $create_time 申请时间
 * @property string|null $update_time 修改时间
 */
class Dis extends BaseModel
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%dis}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['userid', 'campus_id', 'status', 'review_status'], 'integer'],
            [['review_time', 'create_time', 'update_time'], 'safe'],
            [['review_remarks'], 'string'],
            [['name', 'phone'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'userid' => '用户id',
            'campus_id' => '校区id',
            'name' => '姓名',
            'phone' => '手机号',
            'status' => '状态',
            'review_status' => '审核状态',
            'review_time' => '审核时间',
            'review_remarks' => '审核备注',
            'create_time' => '申请时间',
            'update_time' => '修改时间',
        ];
    }

    /**
     * 申请
     * @param $userid 用户id
     * @param $post
     * @return array
     */
    public function apply($userid,$post)
    {
        $result = $this->verific($post);
        if (!$result['success']){
            return $result;
        }
        $model = self::findOne(['userid'=>$userid]);
        if ($model){
            if ($model->review_status==0){
                return commonApi::message('正在审核中...',false);
            }
            if ($model->review_status==1&&$model->status!==1){
                return commonApi::message('禁止申请，请联系客服',false);
            }
        }else{
            $model = new self();
        }
        $model->load($result['data'],'');
        $model->userid = $userid;
        $dis_review = System::getModel()->dis_review??0;
        $user = User::findOne($userid);
        if (!$dis_review){
            $model->status = 1;
            $model->review_status = 1;
            $model->review_time = date('Y-m-d H:i:s');
            $msg = '申请成功';
            $user->dis_status = 1;
        }else{
            $model->status = 0;
            $model->review_status = 0;
            $msg = '提交成功，等待审核';
            $user->dis_status = 0;
        }
        TransformApi::tx(function ()use ($model,$user,$msg){
            if ($model->save()!==false&&$user->save()!==false){
                return commonApi::message($msg);
            }
            throw new Exception('提交失败');
        },$result);
        return $result;

    }

    /**
     * 验证
     * @param $post
     * @return array
     */
    public function verific($post)
    {
        if (empty($post['campus_id'])){
            return commonApi::message('请选择校区',false);
        }
        if (empty($post['name'])){
            return commonApi::message('请输入姓名',false);
        }
        if (!ValidateApi::isChinese($post['name'])){
            return commonApi::message('姓名输入错误',false);
        }
        if (empty($post['phone'])){
            return commonApi::message('请输入手机号',false);
        }
        if (!ValidateApi::isMobilePhone($post['phone'])){
            return commonApi::message('手机号输入错误',false);
        }
        $arr = [
            'campus_id'=>$post['campus_id'],
            'name'=>$post['name'],
            'phone'=>$post['phone'],
        ];
        return commonApi::message('ok',true,$arr);
    }

    /**
     * 开关
     * @param $myUserid 登录用户id
     * @param $userid 操作用户id
     * @return array
     */
    public function switchs($myUserid,$userid)
    {
        $user = User::find()->where(['id'=>$userid])
            ->andWhere(['or',['one_level'=>$myUserid],['second_level'=>$myUserid]])
            ->one();
        if (!$user){
            return commonApi::message('用户不存在',false);
        }
        $dis = Dis::find()->where(['userid'=>$userid])->one();
        if (!$dis){
            return commonApi::message('请先申请',false);
        }
        $dis->status = $user->dis_status==1?0:1;
        $user->dis_status = $user->dis_status==1?0:1;
        TransformApi::tx(function ()use ($user,$dis){
            if ($user->save()&&$dis->save()){
                return commonApi::message('操作成功');
            }
        },$result);

        return $result;
    }
}
